<?php

/**
 * fichecomment actions.
 *
 * @package    s-test
 * @subpackage fichecomment
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class fichecommentActions extends sfActions
{
  public function executeNew(sfWebRequest $request)
  {
    //Permission relative\\
    $this->forward404Unless($this->getUser()->hasPermission("comment_fiche"), "Vous ne pouvez pas commenter de fiche.");

    $this->form = new KTDFicheCommentForm();

    $this->ktd_character = Doctrine::getTable("KTDCharacter")->find(array($request->getParameter("id")));
    $this->forward404Unless($this->ktd_character, "Le personnage de la fiche demandée n'existe pas !");

    $this->ktd_fiche = $this->ktd_character->getFiche();
    $this->forward404Unless($this->ktd_fiche, "La fiche n'existe pas !");
    
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST), "WTF ?! NO POST REQUEST ?! THIS IS MADNESS !");
    $this->forward404Unless($this->getUser()->hasPermission("comment_fiche"), "Vous ne pouvez pas commenter de fiches");

    $this->ktd_character = Doctrine::getTable("KTDCharacter")->find(array($request->getParameter("id")));
    $this->forward404Unless($this->ktd_character, sprintf("Le personnage de la fiche n° %s demandée n'existe pas !", $request->getParameter("id")));

    $this->ktd_fiche = $this->ktd_character->getFiche();
    $this->forward404Unless($this->ktd_fiche, "La fiche n'existe pas !");

    $ktd_fiche_comment = new KTDFicheComment();
    $ktd_fiche_comment->setCharacterId($this->ktd_character->getId());
    $ktd_fiche_comment->setPlayerId($this->getUser()->getId());

    $this->form = new KTDFicheCommentForm($ktd_fiche_comment);

    $this->processForm($request, $this->form, $this->ktd_character);
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($ktd_fiche_comment = Doctrine::getTable('KTDFicheComment')->find(array($request->getParameter('id'))), sprintf('Object ktd_fiche_comment does not exist (%s).', $request->getParameter('id')));
    $this->forward404Unless($this->getUser()->hasRelPermission("edit_comment_fiche", $ktd_fiche_comment->getKTDPlayer()));
    $this->form = new KTDFicheCommentForm($ktd_fiche_comment);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($ktd_fiche_comment = Doctrine::getTable('KTDFicheComment')->find(array($request->getParameter('id'))), sprintf('Object ktd_fiche_comment does not exist (%s).', $request->getParameter('id')));
    $this->form = new KTDFicheCommentForm($ktd_fiche_comment);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $this->forward404Unless($ktd_fiche_comment = Doctrine::getTable('KTDFicheComment')->find(array($request->getParameter('id'))), sprintf('Object ktd_fiche_comment does not exist (%s).', $request->getParameter('id')));
    $this->forward404Unless($this->getUser()->hasRelPermission("edit_comment_fiche", $ktd_fiche_comment->getKTDPlayer()));
    $ktd_fiche = $ktd_fiche_comment->getKTDCharacter()->getFiche();
    $ktd_fiche_comment->delete();

    $this->redirect($this->generateUrl('show_fiche', $ktd_fiche));
  }

  protected function processForm(sfWebRequest $request, sfForm $form, KTDCharacter $ktd_character)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $ktd_fiche_comment = $form->save();
      if($ktd_char != NULL)
        $this->redirect($this->generateUrl("show_fiche", $ktd_character));
      else
          $this->redirect("/");
    }
  }
}
